package com.rayo.accesscontrollercopy.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.rayo.accesscontrollercopy.R;


/**
 * Created by ${Adam} on 16/3/8.
 */
public abstract class BaseFragmentActivity extends FragmentActivity implements View.OnClickListener {
    /**
     * 全局的Context
     */
    protected Context mContext;

    private int messageShowCount = 0;
    private int gcCount = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!ActivityManager.getInstance().containsActivity(this)) {
            ActivityManager.getInstance().putActivity(this.getClass().getSimpleName(), this);
        }
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//        Window window = getWindow();
//        WindowManager.LayoutParams params = window.getAttributes();
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
//        window.setAttributes(params);

        int layoutId = getLayoutId();
        if (layoutId != 0) {
            setContentView(layoutId);
            // 删除窗口背景
            getWindow().setBackgroundDrawable(null);
        }

        mContext = this.getApplicationContext();
        // 向用户展示信息前的准备工作在这个方法里处理
        preliminary();
    }

    /**
     * 向用户展示信息前的准备工作在这个方法里处理
     */
    protected void preliminary() {
        // 初始化组件
        setupViews();

        // 初始化数据
        initialized();
    }

    /**
     * 获取全局的Context
     *
     * @return {@link #mContext = this.getApplicationContext();}
     */
    public Context getContext() {
        return mContext;
    }


    /**
     * @return
     */
    public Activity getActivity() {
        return this;
    }

    /**
     * 布局文件ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化组件
     */
    protected abstract void setupViews();

    /**
     * 初始化数据
     */
    protected abstract void initialized();


    /**
     * 长时间显示Toast提示(来自String)
     *
     * @param view
     * @param message
     * @param snackbarClickListener
     */
    protected void showSnackbar(View view, String message, View.OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    /**
     * 长时间显示Snackbar提示(来自res)
     *
     * @param view
     * @param resId
     * @param snackbarClickListener
     */
    protected void showSnackbar(View view, int resId, View.OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, getString(resId), Snackbar.LENGTH_LONG)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    /**
     * 短暂显示Snackbar提示(来自res)
     *
     * @param view
     * @param resId
     * @param snackbarClickListener
     */
    protected void showShortSnackbar(View view, int resId, View.OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, getString(resId), Snackbar.LENGTH_SHORT)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    /**
     * 短暂显示Snackbar提示(来自String)
     *
     * @param view
     * @param text
     * @param snackbarClickListener
     */
    protected void showShortSnackbar(View view, String text, View.OnClickListener snackbarClickListener) {
        count();
        Snackbar snackbar = Snackbar.make(view, text, Snackbar.LENGTH_SHORT)
                .setAction(getString(R.string.okay), snackbarClickListener);
        setSnackbarMessageTextColor(snackbar, getResources().getColor(R.color.TextWhite));
        snackbar.show();
    }

    public static void setSnackbarMessageTextColor(Snackbar snackbar, int color) {
        View view = snackbar.getView();
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(color);
    }


    private void count() {
        messageShowCount++;
        if (messageShowCount >= gcCount) {
            System.gc();
            messageShowCount = 0;
        }
    }

    /**
     * 通过Action跳转界面
     *
     * @param action
     */
    public void startActivity(String action) {
        if (ActivityManager.getInstance().containsName(action)) {
            return;
        }
        startActivity(action, null);
    }


    /**
     * 含有Bundle通过Action跳转界面
     *
     * @param action
     * @param bundle
     */
    public void startActivity(String action, Bundle bundle) {
        if (ActivityManager.getInstance().containsName(action)) {
            return;
        }
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        if (ActivityManager.getInstance().containsName(cls.getSimpleName())) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    /**
     * 带有右进右出动画的退出
     */
    @Override
    public void finish() {
        super.finish();
    }

    /**
     * 默认退出
     */
    public void defaultFinish() {
        super.finish();
        ActivityManager.getInstance().removeActivity(this.getClass().getSimpleName());
    }

    protected void replaceFragment(int containerViewId, Fragment fragment, String backStack) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        ft.addToBackStack(backStack);
        ft.replace(containerViewId, fragment);
        ft.commit();
    }
}
